package com.trxy.hotelsystem.contorller;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.trxy.hotelsystem.model.repertory;
import com.trxy.hotelsystem.model.staffInf;
import com.trxy.hotelsystem.model.warehouseRecord;
import com.trxy.hotelsystem.service.impl.RepertoryServiceImpl;
import com.trxy.hotelsystem.service.impl.Warehouse_recordseviceImpl;

import frame.uitls.PrimaryKeyUtil;
import net.sf.json.JSONArray;

@Controller
public class RepertoryController {
	
	@Autowired
	private RepertoryServiceImpl repertoryServiceImpl;
	
	@Autowired
	private Warehouse_recordseviceImpl WarehouserecordseviceImpl;
	
	//仓库查询
	@RequestMapping(value = "/queryRepertoryAll.do")
	public String qureyRepertoryAll(HttpServletResponse response,HttpServletRequest request,
			Model model,repertory repertory,
			@RequestParam(defaultValue="1")Integer pageNum,@RequestParam(defaultValue="7")Integer pageSize) throws UnsupportedEncodingException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		//开启分页查询
		PageHelper.startPage(pageNum, pageSize);
		
		List<repertory> list = repertoryServiceImpl.queryRepertoryAll();
		
		PageInfo<repertory> page = new PageInfo<>(list);
		
		model.addAttribute("pageInfo", page);
		
		return "view/repertory/repertory";
	}
	
	//通过商品名称查询商品
	@RequestMapping(value = "/selectRepertoryByName.do")
	public String selectRepertoryByName(HttpServletResponse response,HttpServletRequest request,
			Model model,repertory repertory,
			@RequestParam(defaultValue="1")Integer pageNum,@RequestParam(defaultValue="7")Integer pageSize) throws Exception {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		//开启分页查询
	    PageHelper.startPage(pageNum, pageSize);
		
		String repertoryName = "";
		String repName;
		repertoryName = request.getParameter("repertoryName");	
		
		if(repertoryName != "") {
			repName = repertoryName.trim();
			repertory.setProduct(repName);
			
			List<repertory> list = repertoryServiceImpl.selectRepertoryByName(repertory);
			
			PageInfo<repertory> page = new PageInfo<>(list);
			
			model.addAttribute("pageInfo", page);
			
			return "view/repertory/repertory";
			
		}else {
            List<repertory> list = repertoryServiceImpl.queryRepertoryAll();
			
			PageInfo<repertory> page = new PageInfo<>(list);
			
			model.addAttribute("pageInfo", page);
			
			return "view/repertory/repertory";
		}
		
	}
	
	
	//通过商品分类查询商品
	@RequestMapping(value = "/selectRepertoryByClassify.do")
	public String selectRepertoryByClassify(HttpServletResponse response,HttpServletRequest request,
			Model model,repertory repertory,
			@RequestParam(defaultValue="1")Integer pageNum,@RequestParam(defaultValue="7")Integer pageSize) throws Exception {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		//开启分页查询
	    PageHelper.startPage(pageNum, pageSize);
	    
	    String repertoryClassify = "";
	    String repClassify;
	    repertoryClassify = request.getParameter("repertoryClassify");
	    
	    if(repertoryClassify != "") {
	    	repClassify = repertoryClassify.trim();
			repertory.setClassify(repClassify);
			
			List<repertory> list = repertoryServiceImpl.selectRepertoryByClassify(repertory);
		
			
			PageInfo<repertory> page = new PageInfo<>(list);
			
			model.addAttribute("pageInfo", page);
			
			return "view/repertory/repertory";
			
		}else {
            List<repertory> list = repertoryServiceImpl.queryRepertoryAll();
			
			PageInfo<repertory> page = new PageInfo<>(list);
			
			model.addAttribute("pageInfo", page);
			
			return "view/repertory/repertory";
		}
	}
	
	//入库模态框数据
	@RequestMapping(value = "/addRepertory.do")
	public void addRepertory(HttpServletResponse response,HttpServletRequest request,
			Model model,repertory repertory) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		PrintWriter pw = response.getWriter();
		
		String id = request.getParameter("id");
		repertory.setReserved1(id);

		
		List<repertory> list = repertoryServiceImpl.selectRepertoryById(repertory);
		
		if(list.size() > 0) {
			
			String st = JSONArray.fromObject(list).toString();
			response.getWriter().write(st);
		}else {
			pw.write("0");
		}	
	}
	
	//入库
	@RequestMapping(value = "addReper.do")
	public void addReper(HttpServletResponse response,HttpServletRequest request, HttpSession session,
			Model model,repertory repertory,warehouseRecord warehouserecord) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		PrintWriter pw = response.getWriter();
		
		//获取参数
		String repId = request.getParameter("repId");
		String addQuantity = request.getParameter("addQuantity");
		repertory.setReserved1(repId);
		
		//定义总数
		int sum;
		
		//查询商品原有数量并叠加
		List<repertory> list = repertoryServiceImpl.selectRepertoryById(repertory);
		for(repertory one:list) {
			
			//装箱
			warehouserecord.setProduct(one.getProduct());
			warehouserecord.setClassify(one.getClassify());
			warehouserecord.setNature("入库");
			warehouserecord.setTime(new Date());
			warehouserecord.setRemark(one.getRemark());
			warehouserecord.setPrice(one.getPrice());
			warehouserecord.setReserved1(PrimaryKeyUtil.getPrimaryKey());
			staffInf us = (staffInf) session.getAttribute("loginUser");
			warehouserecord.setReserved2(us.getName());
			//新入库商品数量增加
			int quantity = one.getQuantity();
			sum = quantity + Integer.parseInt(addQuantity);
			
			repertory.setQuantity(sum);
		}
		
		//入库
		int i = repertoryServiceImpl.addReper(repertory);
		if(i > 0) {
			warehouserecord.setQuantity(Integer.parseInt(addQuantity));
			//添加入库记录
			int j = WarehouserecordseviceImpl.insertWarehouse_record(warehouserecord);
			if(j > 0) {
				pw.write("1");
			}else {
				pw.write("0");
			}
			
		}else {
			pw.write("0");
		}
		
		
	}
	
	//出库
	@RequestMapping(value = "/stockRemoval.do")
	public void stockRemoval(HttpServletResponse response,HttpServletRequest request, HttpSession session,
			Model model,repertory repertory,warehouseRecord warehouserecord) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		PrintWriter pw = response.getWriter();
		
		String repId = request.getParameter("repId");
		String moveQuantity = request.getParameter("moveQuantity");
		repertory.setReserved1(repId);
		
		int sum;
		
		List<repertory> list = repertoryServiceImpl.selectRepertoryById(repertory);
		
		if(list.size() > 0 ) {
			for(repertory one:list) {
				sum = one.getQuantity() - Integer.parseInt(moveQuantity);
				repertory.setQuantity(sum);
				
				warehouserecord.setProduct(one.getProduct());
				warehouserecord.setClassify(one.getClassify());
				warehouserecord.setNature("出库");
				warehouserecord.setTime(new Date());
				warehouserecord.setRemark(one.getRemark());
				warehouserecord.setPrice(one.getPrice());
				warehouserecord.setReserved1(PrimaryKeyUtil.getPrimaryKey());
				staffInf us = (staffInf) session.getAttribute("loginUser");
				warehouserecord.setReserved2(us.getName());
				
			}
		}
		
		int i = repertoryServiceImpl.addReper(repertory);
		warehouserecord.setQuantity(Integer.parseInt(moveQuantity));
		
		if(i > 0) {
			int j = WarehouserecordseviceImpl.insertWarehouse_record(warehouserecord);
			
			if(j > 0) {
				pw.write("1");
			}else {
				pw.write("0");
			}
		}
		
		
	}
	
	//通过ID查询仓库
	@RequestMapping(value = "/selectRepertoryById.do")
	public void selectRepertoryById(HttpServletResponse response,HttpServletRequest request, HttpSession session,
			Model model,repertory repertory) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		String id = request.getParameter("id");

		repertory.setReserved1(id);
		
		List<repertory> list = repertoryServiceImpl.selectRepertoryById(repertory);
		
		String st = JSONArray.fromObject(list).toString();
		response.getWriter().write(st);
		
		
	}
	
	//修改商品信息
	@RequestMapping(value = "alterRepertory.do")
	public void alterRepertory(HttpServletResponse response,HttpServletRequest request, HttpSession session,
			Model model,repertory repertory) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		PrintWriter pw = response.getWriter();
		
		//获取参数
		String reId = request.getParameter("reId");
		String repProduct = request.getParameter("repProduct");
		String repclassify = request.getParameter("repclassify");
		String repPrice = request.getParameter("repPrice");
		String repRemark = request.getParameter("repRemark");
		
		repertory.setReserved1(reId);
		repertory.setProduct(repProduct);
		repertory.setClassify(repclassify);
		repertory.setPrice(Double.parseDouble(repPrice));
		repertory.setRemark(repRemark);
		
		int i = repertoryServiceImpl.updateRepertoryById(repertory);
		
		if(i > 0) {
			pw.write("1");
		}else {
			pw.write("0");
		}
		
	}
	
	//出入库查询——全查询
	@RequestMapping(value = "/queryWarehouseRecordAll.do")
	public String queryWarehouseRecordAll(HttpServletResponse response,HttpServletRequest request, HttpSession session,
			Model model,warehouseRecord warehouserecord,
			@RequestParam(defaultValue="1")Integer pageNum,@RequestParam(defaultValue="7")Integer pageSize) throws UnsupportedEncodingException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		 PageHelper.startPage(pageNum, pageSize);
		
		List<warehouseRecord> list = WarehouserecordseviceImpl.queryWarehouseRecordAll();
		
		for(warehouseRecord one:list) {
			SimpleDateFormat format0 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        String time = format0.format(one.getTime());
	        one.setReserved3(time);
		}
		
		PageInfo<warehouseRecord> page = new PageInfo<>(list);
		
		model.addAttribute("pageInfo", page);
		
		return "view/repertory/warehouse_record";
	}
	
	//条件查询出入库记录
	@RequestMapping(value = "/selectWarehouseRecord.do")
	public String selectWarehouseRecord(HttpServletResponse response,HttpServletRequest request, HttpSession session,
			Model model,warehouseRecord warehouserecord,
			@RequestParam(defaultValue="1")Integer pageNum,@RequestParam(defaultValue="7")Integer pageSize) throws UnsupportedEncodingException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		PageHelper.startPage(pageNum, pageSize);
		
		String executor = request.getParameter("executor");
		String product = request.getParameter("product");
		String classify = request.getParameter("classify");
		String selectText = request.getParameter("selectText");
		
		if(executor != null || executor != "") {
			String executor2 = executor.trim();
			warehouserecord.setReserved2(executor2);
		}
		if(product != null || product != "") {
			String product2 = product.trim();
			warehouserecord.setProduct(product2);
		}
		if(classify != null || classify != "") {
			String classify2 = classify.trim();
			warehouserecord.setClassify(classify2);
		}
		if(selectText.equals("----默认----")) {
			warehouserecord.setNature(null);
			
		}else if(selectText.equals("----出库----")) {
			warehouserecord.setNature("出库");
		}else {
			warehouserecord.setNature("入库");
		}		
		
		List<warehouseRecord> list = WarehouserecordseviceImpl.selectWarehouseRecord(warehouserecord);
		
        PageInfo<warehouseRecord> page = new PageInfo<>(list);
		
		model.addAttribute("pageInfo", page);
		
		model.addAttribute("executor",executor);
		model.addAttribute("product",product);
		model.addAttribute("classify",classify);
		model.addAttribute("selectText",selectText);
		model.addAttribute("inf","多条件");
	
		return "view/repertory/warehouse_record";
		
	}
	
	//添加库存
	@RequestMapping(value = "addRepertory2.do")
	public void addRepertory(HttpServletResponse response,HttpServletRequest request,HttpSession session, 
			repertory retory,warehouseRecord warehouserecord) throws IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		PrintWriter pw = response.getWriter();
		
		String product = request.getParameter("product");
		String classify = request.getParameter("classify");
		String quantity = request.getParameter("quantity");
		String remarks = request.getParameter("remarks");
		
		String id = PrimaryKeyUtil.getPrimaryKey();
		
		retory.setProduct(product);
		retory.setClassify(classify);
		retory.setRemark(remarks);
		retory.setReserved1(id);
		
		int i = repertoryServiceImpl.addRepertory(retory);
		
		int sum;
		
		if(i > 0) {
			
			//查询商品原有数量并叠加
			List<repertory> list = repertoryServiceImpl.selectRepertoryById(retory);
			for(repertory one:list) {
				
				//装箱
				warehouserecord.setProduct(one.getProduct());
				warehouserecord.setClassify(one.getClassify());
				warehouserecord.setNature("入库");
				warehouserecord.setTime(new Date());
				warehouserecord.setRemark(one.getRemark());
				warehouserecord.setReserved1(PrimaryKeyUtil.getPrimaryKey());
				staffInf us = (staffInf) session.getAttribute("loginUser");
				warehouserecord.setReserved2(us.getName());
				//新入库商品数量增加
				int quantit = one.getQuantity();
				sum = quantit + Integer.parseInt(quantity);
				
				retory.setQuantity(sum);
			}
			
			//入库
			int j = repertoryServiceImpl.addReper(retory);
			
			warehouserecord.setQuantity(Integer.parseInt(quantity));
			//添加入库记录
			int k = WarehouserecordseviceImpl.insertWarehouse_record(warehouserecord);
			if(j > 0 && k > 0) {
				pw.write("1");
			}else {
				pw.write("0");
			}
			
		}else {
			pw.write("0");
		}

	}


}
